package com.assignwork.service.impl;

import com.assignwork.dto.WorkArrangeFixedMachineSubmitDTO;
import com.assignwork.dto.WorkArrangeShipUnderMachineSubmitDTO;
import com.assignwork.entity.WorkArrangeFixedMachine;
import com.assignwork.entity.WorkArrangeShipCabinInfo;
import com.assignwork.entity.WorkArrangeShipUnderMachine;
import com.assignwork.mapper.WorkArrangeShipUnderMachineMapper;
import com.assignwork.service.IWorkArrangeShipUnderMachineService;
import com.assignwork.vo.WorkArrangeShipUnderMachineVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName WorkArrangeShipUnderMachineServiceImpl
 * @Description 航运配工下舱机械
 * @Author dujianyu
 * @Date 2023/12/22 12:30
 */
@Service
public class WorkArrangeShipUnderMachineServiceImpl extends BaseServiceImpl<WorkArrangeShipUnderMachineMapper, WorkArrangeShipUnderMachine> implements IWorkArrangeShipUnderMachineService {

    private List<WorkArrangeShipUnderMachineVO> dataProcess(List<WorkArrangeShipUnderMachineVO> data) {
        for (WorkArrangeShipUnderMachineVO vo : data) {
            vo.setMachineKindName(DictBizCache.getValue("machine_kind", vo.getMachineKind()));
        }
        return data;
    }

    @Override
    public List<WorkArrangeShipUnderMachineVO> infoByCabinInfoId(Long cabinInfoId) {
        List<WorkArrangeShipUnderMachineVO> data = baseMapper.selectInfoByCabinInfoId(cabinInfoId);
        return dataProcess(data);
    }



    @Override
    public boolean submitWaShipUnderMachine(Long arrangeId, Long cabinInfoId, List<WorkArrangeShipUnderMachineSubmitDTO> underMachines) {
        if (!Func.isNull(underMachines) && !Func.isEmpty(underMachines)) {
            List<WorkArrangeShipUnderMachine> entities = new ArrayList<>();
            for (WorkArrangeShipUnderMachineSubmitDTO item : underMachines) {
                WorkArrangeShipUnderMachine newUnderMachine = new WorkArrangeShipUnderMachine();
                newUnderMachine.setArrangeId(arrangeId);
                newUnderMachine.setCabinInfoId(cabinInfoId);
                newUnderMachine.setMachineCorp(item.getMachineCorp());
                newUnderMachine.setMachineKind(item.getMachineKind());
                newUnderMachine.setAssemblyLineId(item.getAssemblyLineId());
                newUnderMachine.setPlanId(item.getPlanId());
                newUnderMachine.setRemark(item.getRemark());
                if (item.getId() != null) {
                    newUnderMachine.setId(item.getId());
                    newUnderMachine.setIsDeleted(0);
                }
                entities.add(newUnderMachine);
            }
            return super.saveOrUpdateBatch(entities);
        } else {
            return true;
        }
    }

    @Override
    public boolean removeByArrangeId(Long arrangeId) {
        QueryWrapper<WorkArrangeShipUnderMachine> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeShipUnderMachine::getId)
                .eq(WorkArrangeShipUnderMachine::getArrangeId,arrangeId)
                .eq(WorkArrangeShipUnderMachine::getIsDeleted,0);
        List<WorkArrangeShipUnderMachine> entities = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(entities)) {
            List<Long> ids = entities.stream().map(WorkArrangeShipUnderMachine::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
        return true;
    }

    @Override
    public boolean removeByArrangeId(Long arrangeId, Set<Long> exclude) {
        QueryWrapper<WorkArrangeShipUnderMachine> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeShipUnderMachine::getId)
                .eq(WorkArrangeShipUnderMachine::getArrangeId,arrangeId)
                .notIn(Func.isNotEmpty(exclude), WorkArrangeShipUnderMachine::getId, exclude)
                .eq(WorkArrangeShipUnderMachine::getIsDeleted,0);
        List<WorkArrangeShipUnderMachine> entities = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(entities)) {
            List<Long> ids = entities.stream().map(WorkArrangeShipUnderMachine::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
        return true;
    }

    @Override
    public List<WorkArrangeShipUnderMachineVO> infoByArrangeId(Long arrangeId) {
        List<WorkArrangeShipUnderMachineVO> data = baseMapper.selectInfoByArrangeId(arrangeId);
        return dataProcess(data);
    }


}
